{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deutsch's Algorithm "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Deutsch's Algorithm is a concise demonstration of the differences in computational complexity between classical and quantum algorithms for certain problems.  For Desutch's algorithm, we begin with a function which takes in a bit and outputs a bit. This can be represented as $f: \\{0,1\\} \\longrightarrow \\{0,1\\}$. \n",
    "The function $f$ has the property that it either constant or balanced. The goal of Deutsch's Algorithm is to determine whether our given function is constant or whether it is balanced. \n",
    "\n",
    "A constant function is \"A balanced function is a function such that the outputs are the same regardless of the inputs, i.e., if $f(0) = 0$ then $f(1) = 1$ or if $f(0) = 1$ then $f(1) = 0$.\n",
    "\", the outputs are the same regardless of the inputs, i.e., in the case of $f: \\{0,1\\} \\longrightarrow \\{0,1\\}$, there are are two ways in which this can occur: $f(0) = f(1) = 0$ or $f(0) = f(1) = 1$.\n",
    "\n",
    "A balanced function is defined such that the ouputs are balanced across their possibilities, i.e., if $f(0) = 0$ then $f(1) = 1$ or if $f(0) = 1$ then $f(1) = 0$.\n",
    " \n",
    "Classically, if we are given a function $f: \\{0,1\\} \\longrightarrow \\{0,1\\}$, we can determine if it is constant or balanced by evaluating the function at $0$ and at $1$.  This is carried out in the code below: \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Treat the code snippet below like a black box, i.e., you don't have access to the value of the property argument and can just query f with different inputs.\n",
    "\n",
    "\n",
    "def f(x, property='constant'):\n",
    "\n",
    "    if property == 'constant':\n",
    "\n",
    "        # The output is a 1 regardless of the input; we can also make the output to be 0.\n",
    "\n",
    "        if x == 0:\n",
    "            return 1\n",
    "        elif x == 1:\n",
    "            return 1\n",
    "\n",
    "    if property == 'balanced':\n",
    "\n",
    "        # The output depends on the input.\n",
    "\n",
    "        if x == 0:\n",
    "            return 1\n",
    "        elif x == 1:\n",
    "            return 0"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us now classically find out if the function we defined above is constant or balanced. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The function is constant\n"
     ]
    }
   ],
   "source": [
    "if f(0) == 0:\n",
    "    if f(1) == 0:\n",
    "        print('The function is constant')\n",
    "    elif f(1) == 1:\n",
    "        print('The function is balanced')\n",
    "\n",
    "elif f(0) == 1:\n",
    "    if f(1) == 1:\n",
    "        print('The function is constant')\n",
    "    elif f(1) == 0:\n",
    "        print('The function is balanced')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you step through the `if` statements above, you may notice that we require 2 calls to the function to determine its property. That is, we have to query $f$ twice.\n",
    "\n",
    "The claim is that Deutsch's Algorithm can determine if a given function is constant or balanced with just 1 function evalulation, demonstrating quantum advantage. \n",
    "\n",
    "Below we first outline Deutsch's Algorithm and work through the math to verify that it does as promised. Then, we provide the implementation in CUDA-Q. \n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## XOR $\\oplus$\n",
    "\n",
    "You may have seen the $\\oplus$ symbol floating around. This is the exclusive OR or the XOR operation. It follows the rules of addition modulo 2. For example, \n",
    "\n",
    "\n",
    "1. $3\\oplus5 = (3+5)_{\\%2} = 8_{\\%2} = 0$\n",
    "\n",
    "2. $5\\oplus4 = (5+4)_{\\%2} = 9_{\\%2} = 1$\n",
    "\n",
    "A quick way to perform the $\\oplus$ calculation is if the result of the addition is an even number, the result is 0 otherwise 1. "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quantum oracles\n",
    "\n",
    "<img src=\"images/oracle.png\"  width=\"300\" height=\"150\">\n",
    "\n",
    "Suppose we have $f(x): \\{0,1\\} \\longrightarrow \\{0,1\\}$. We can compute this function on a quantum computer using oracles which we treat as black box functions that yield the output with an appropriate sequence of logical gates. \n",
    "\n",
    "Above you see an oracle represented as $U_f$ which allows us to transform the state $\\ket{x}\\ket{y}$ into: \n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "U_f\\ket{x}\\ket{y}  =  \\ket{x}\\ket{y \\oplus f(x)}\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "If $y = 0$, then $U_f\\ket{x}\\ket{y} = U_f\\ket{x}\\ket{0}  =   \\ket{x}\\ket{0 \\oplus f(x)} =  \\ket{x}\\ket{f(x)}$, since $f(x)$ can either be $0$ or $1$ and $0 \\oplus 0 = 0$ and $0 \\oplus 1 = 1$.\n",
    "\n",
    "This is remarkable because by setting $\\ket{y} = \\ket{0}$, we can extract the value of $f(x)$ by measuring the value of the second qubit. \n",
    " \n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Phase oracle\n",
    "\n",
    "\n",
    "Since $f(x)$ can be either $0$ or $1$, we have that $0 \\oplus f(x) = 0 \\oplus 0 =  0 = f(x)$ or $0 \\oplus f(x) = 0 \\oplus 1 =  1 = f(x)$. \n",
    "\n",
    "Similarly, $1 \\oplus f(x) = 1 \\oplus 0 =  1 = \\overline{f(x)}$ or $1 \\oplus f(x) = 1 \\oplus 1 =  0 = \\overline{f(x)}$ where the bar on top of $f(x)$ denotes 'not $f(x)$'. \n",
    "\n",
    "\n",
    "Let us now prove a result which we shall use later: \n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "U_f\\ket{x}\\ket{-}  &=   U_f \\ket{x} \\tfrac{1}{\\sqrt{2}}(\\ket{0} - \\ket{1}) \\\\\n",
    "&= \\tfrac{1}{\\sqrt{2}}  (U_f\\ket{x}\\ket{0} - U_f\\ket{x}\\ket{1}) \\\\\n",
    "&= \\tfrac{1}{\\sqrt{2}}  (\\ket{x}\\ket{0 \\oplus f(x)} - \\ket{x}\\ket{1 \\oplus f(x)}) \\\\\n",
    "&= \\tfrac{1}{\\sqrt{2}}  (\\ket{x}\\ket{f(x)} - \\ket{x}\\ket{\\overline{f(x)}}) \\\\\n",
    "&= \n",
    "\\begin{cases}\n",
    "      \\tfrac{1}{\\sqrt{2}}  (\\ket{x}\\ket{0} - \\ket{x}\\ket{1}) = \\ket{x}\\ket{-} &   f(x) = 0 \\\\\n",
    "      \\tfrac{1}{\\sqrt{2}}  (\\ket{x}\\ket{1} - \\ket{x}\\ket{0}) = -\\ket{x}\\ket{-} &   f(x) = 1 \\\\\n",
    "\\end{cases} \\\\\n",
    "&= (-1)^{f(x)}\\ket{x}\\ket{-}    \n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "\n",
    "In summary, $U_f\\ket{x}\\ket{-} = (-1)^{f(x)}\\ket{x}\\ket{-}$ where the $\\ket{-}$ qubit was left unchanged and a phase was applied to $\\ket{x}$. \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Quantum parallelism\n",
    "\n",
    "Consider: \n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "U_f\\ket{+}\\ket{0} &= U_f \\tfrac{1}{\\sqrt{2}}(\\ket{0}+\\ket{1}) \\ket{0} \\\\\n",
    "&= U_f \\tfrac{1}{\\sqrt{2}}(\\ket{0}\\ket{0}+\\ket{1}\\ket{0}) \\\\\n",
    "&= \\tfrac{1}{\\sqrt{2}}(U_f \\ket{0}\\ket{0} + U_f \\ket{1}\\ket{0})  \\\\   \n",
    "&= \\tfrac{1}{\\sqrt{2}}(U_f \\ket{0}\\ket{0} + U_f \\ket{1}\\ket{0})  \\\\   \n",
    "&= \\tfrac{1}{\\sqrt{2}}(\\ket{0}\\ket{0 \\oplus f(0)} + \\ket{1}\\ket{0 \\oplus f(1)})  \\\\   \n",
    "&= \\tfrac{1}{\\sqrt{2}}(\\ket{0}\\ket{f(0)} + \\ket{1}\\ket{f(1)})  \\\\   \n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "We have calculated information about both $f(0)$ and $f(1)$ simultaneously. Quantum mechanics allows for parallelism by exploiting the ability of superposition states.  "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deutsch's Algorithm: \n",
    "\n",
    "Our aim is to find out if $f: \\{0,1\\} \\longrightarrow \\{0,1\\}$ is a constant or a balanced function? If constant, $f(0) = f(1)$, and if balanced, $f(0) \\neq f(1)$.\n",
    "\n",
    "We step through the circuit diagram below and follow the math after the application of each gate.\n",
    "\n",
    "<img src=\"images/deutsch.png\"  width=\"500\" height=\"210\">\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "$$\n",
    "\\ket{\\psi_0}  =  \\ket{01}\n",
    "\\tag{1}\n",
    "$$\n",
    "\n",
    "\n",
    "$$\\ket{\\psi_1}  =   H_1H_0\\ket{\\psi_0} = H_1H_0\\ket{01} = \\ket{+-} = \\frac{1}{\\sqrt{2}} (\\ket{0}+\\ket{1})\\ket{-}\n",
    "\\tag{2}\n",
    "$$\n",
    "\n",
    "\n",
    "\n",
    "$$\n",
    "\\ket{\\psi_2}  =   U_f\\ket{\\psi_1} = \\frac{1}{\\sqrt{2}} (U_f\\ket{0}\\ket{-} + U_f\\ket{1}\\ket{-})\n",
    "\\tag{3}\n",
    "$$\n",
    "\n",
    "using $U_f\\ket{x}\\ket{-} = (-1)^{f(x)}\\ket{x}\\ket{-}$ which we proved in the phase oracle section above: \n",
    "\n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "\\ket{\\psi_2}  =  \\frac{1}{\\sqrt{2}} ((-1)^{f(0)}\\ket{0}\\ket{-} + (-1)^{f(1)}\\ket{1}\\ket{-}) \\\\\n",
    "= \\frac{1}{\\sqrt{2}} ((-1)^{f(0)}\\ket{0} + (-1)^{f(1)}\\ket{1}) \\ket{-} \\\\\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "We now drop the second, qubit in the $\\ket{-}$ state from our derivations as the figure above has no further gate operations acting on it. Remember our aim is to find out if $f(x)$ is a constant or a balanced function. We have the following options: \n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\ket{\\psi_{2}}=\n",
    "\\begin{cases}\n",
    "      \\tfrac{1}{\\sqrt{2}} (\\ket{0} + \\ket{1})  &   f(0) = 0, f(1) = 0, f(x)  \\text{ is constant} \\\\\n",
    "      -\\tfrac{1}{\\sqrt{2}} (\\ket{0} + \\ket{1})  &   f(0) = 1, f(1) = 1, f(x)  \\text{ is constant} \\\\\n",
    "      \\tfrac{1}{\\sqrt{2}} (\\ket{0} - \\ket{1})  &   f(0) = 0, f(1) = 1, f(x)  \\text{ is balanced} \\\\\n",
    "      -\\tfrac{1}{\\sqrt{2}} (\\ket{0} - \\ket{1})  &   f(0) = 1, f(1) = 0, f(x)  \\text{ is balanced} \\\\\n",
    "\\end{cases} \n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "\\ket{\\psi_{2}} =\n",
    "\\begin{cases}\n",
    "      \\pm \\tfrac{1}{\\sqrt{2}} (\\ket{0} + \\ket{1}) = \\pm \\ket{+}  &    f(x)  \\text{ is constant} \\\\\n",
    "      \\pm \\tfrac{1}{\\sqrt{2}} (\\ket{0} - \\ket{1})= \\pm \\ket{-}  &    f(x)  \\text{ is balanced} \\\\\n",
    "\\end{cases} \n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "\\ket{\\psi_{3}}= H\\ket{\\psi_{2}}\n",
    "\\begin{cases}\n",
    "      \\pm H\\ket{+} =  \\pm \\ket{0} &    f(x)  \\text{ is constant} \\\\\n",
    "      \\pm H\\ket{-}  = \\pm \\ket{1}  &   f(x)  \\text{ is balanced} \\\\\n",
    "\\end{cases} \n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "We can now measure the first qubit to yield either a $0$ or a $1$ to determine if $f(x)$ is constant or balanced. \n",
    "\n",
    "Deutsch's algorithm may not be practically useful but does demonstrate quantum advantage since it uses one function call to determine the property of $f$ in comparison to two for classical methods. \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the CUDA-Q package and set the target to run on NVIDIA GPUs.\n",
    "\n",
    "import cudaq\n",
    "import numpy as np\n",
    "\n",
    "from typing import List\n",
    "\n",
    "cudaq.set_target(\"nvidia\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Here we input the values of [f(0), f(1)] which allows us to represent constant or balanced functions.\n",
    "\n",
    "fx = [0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     ╭───╮          ╭───╮\n",
      "q0 : ┤ h ├───────●──┤ h ├\n",
      "     ├───┤╭───╮╭─┴─╮╰───╯\n",
      "q1 : ┤ x ├┤ h ├┤ x ├─────\n",
      "     ╰───╯╰───╯╰───╯     \n",
      "\n",
      "f(x) is a balanced function\n"
     ]
    }
   ],
   "source": [
    "# Let us now code up the circuit shown above following the state Psi after each step.\n",
    "\n",
    "qubit_count = 2\n",
    "\n",
    "\n",
    "@cudaq.kernel\n",
    "def kernel(fx: List[int]):\n",
    "    qubit_0 = cudaq.qubit()\n",
    "    qubit_1 = cudaq.qubit()\n",
    "\n",
    "    # Psi 0\n",
    "    x(qubit_1)\n",
    "\n",
    "    # Psi 1\n",
    "    h(qubit_0)\n",
    "    h(qubit_1)\n",
    "\n",
    "    # Psi 2 - oracle\n",
    "    if fx[0] == 1:\n",
    "        x.ctrl(qubit_0, qubit_1)\n",
    "        x(qubit_1)\n",
    "\n",
    "    if fx[1] == 1:\n",
    "        x.ctrl(qubit_0, qubit_1)\n",
    "\n",
    "    # Psi 3\n",
    "    h(qubit_0)\n",
    "\n",
    "    # Measure the qubit to yield if the function is constant or balanced.\n",
    "    mz(qubit_0)\n",
    "\n",
    "\n",
    "print(cudaq.draw(kernel, fx))\n",
    "\n",
    "result = cudaq.sample(kernel, fx, shots_count=1)\n",
    "\n",
    "if np.array(result)[0] == '0':\n",
    "    print('f(x) is a constant function')\n",
    "elif np.array(result)[0] == '1':\n",
    "    print('f(x) is a balanced function')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This algorithm can be generalized to determine if a $n$-bit function $f:{0,1}^n\\longrightarrow {0,1}$ is constant or a balanced with only $\\frac{n}{2}$ function evaluations, for $n$ even.  A function if balanced if half of the inputs map to $0$ and half map to $1$.  \n",
    "\n",
    "Here we must assume that the function that we are given is either constant or balanced since there are $n$-bit functions that are neither constant, nor balanced.  For instance the $2$-bit function $f(b_0,b_1) = \\max(b_0,b_1)$ is neither balanced, nor constant.\n",
    "\n",
    "A hint on how you might approach this problem is to first solve the problem for $n=2$ and see if you can then use that approach to handle $n$-bit functions for larger values of $n$."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
